# _*_ coding: utf-8 _*_
from odoo import models, fields, api


class StockPutOnShelfPickQuant(models.TransientModel):
    _name = "stock_put_on_shelf.pick_quant"

    def get_quant_domain(self):
        domain = []
        put = self.get_record()
        if put:
            lot_ids = put.mapped("line_ids.lot_id")
            if lot_ids:
                domain = [('lot_id', 'not in', lot_ids.ids)]
        return domain

    # todo 这个过滤还不准的
    quant_ids = fields.Many2many("stock.quant.for.pick", domain=get_quant_domain)
    # not_allowed_quant_ids = fields.Many2many()

    @api.model
    def get_record(self):
        if self.env.context.get('active_model') and self.env.context.get('active_id'):
            return self.env[self.env.context.get('active_model')].browse(self.env.context.get('active_id'))

    def action_ok(self):
        put = self.get_record()
        put.write({
            "line_ids": [(0, 0, {
                "product_id": q.product_id.id,
                "lot_id": q.lot_id.id,
                "product_qty": q.product_qty,
                "src_location_id":q.location_id.id,
                "dest_location_id":self.env.ref("stock.stock_location_stock").id
            }) for q in self.quant_ids]
        })
